﻿using DataBase;
using DataBase.Utils;
using GreenXi.Model;
using Prism.Commands;
using System;
using System.Collections.ObjectModel;
using System.Linq;

namespace GreenXi.ViewModel
{
    public class DecisionViewModel : DecisionModel
    {
        public Action CloseAction { get; set; }
        public static int? id { get; set; }
        public static string PressId { get; set; }
        public DecisionViewModel(int? ids, string PressIds)
        {
            id = ids;
            PressId = PressIds;
            ImagePaths = new ObservableCollection<DecisionType>();
            var ImagePathsList = MyDbContext.Instance.GetDecisionType();
            foreach (var item in ImagePathsList)
            {
                ImagePaths.Add(item);
            }
            ImageClickCommand = new DelegateCommand<object>((p) => { OnImageClick(p); });


        }
        private void OnImageClick(object parameter)
        {
            using MyDbContext myDbContext = new();
            var parameters = parameter as DecisionType;
            var decision = myDbContext.Decisions.FirstOrDefault(x => x.ID == parameters.Id);
            decision.Type = parameters.Name;
            decision.Image = parameters.Image;
            if (id != 0)
            {
                decision.ID = Convert.ToInt32(id);
                decision.PressId = PressId;
                myDbContext.DecisionUpdate(decision);
            }
            else
            {
                decision.X1 = 10;
                decision.X2 = 20;
                decision.Y1 = 20;
                decision.Y2 = 50;
                myDbContext.DecisionAdd(decision);
            }
            CloseAction();//关闭弹窗

        }

    }
}
